package com.kasa.zto;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2008</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class ZTOLinkedHashMap<K extends ZTOType, V extends ZTOType> extends LinkedHashMap<K, V> implements ZTOType
{
    private static final long serialVersionUID = -7515815855975080790L;

    private Class<? extends K> keyClass;
    private Class<? extends V> valueClass;

    public ZTOLinkedHashMap(Class<? extends K> keyClass, Class<? extends V> valueClass)
    {
        super();
        this.keyClass = keyClass;
        this.valueClass = valueClass;
    }

    public ZTOLinkedHashMap(int initialCapacity, Class<? extends K> keyClass, Class<? extends V> valueClass)
    {
        super(initialCapacity);
        this.keyClass = keyClass;
        this.valueClass = valueClass;
    }

    public ZTOLinkedHashMap(Map<? extends K, ? extends V> m, Class<? extends K> keyClass, Class<? extends V> valueClass)
    {
        super(m);
        this.keyClass = keyClass;
        this.valueClass = valueClass;
    }

    public void encode(ZTOEncoder encoder) throws ZTOException
    {
        int size = this.size();
        encoder.encode_int(size);
        Iterator<K> it = this.keySet().iterator();
        K k;
        while (it.hasNext())
        {
            k = it.next();
            encoder.encodeZTOType(k);
            encoder.encodeZTOType(this.get(k));
        }
    }

    public void decode(ZTODecoder decoder) throws ZTOException
    {
        int size = decoder.decode_int();
        K k;
        V v;
        for (int i = 0; i < size; i++)
        {
            k = decoder.decodeZTOType(keyClass);
            v = decoder.decodeZTOType(valueClass);
            this.put(k, v);
        }
    }

	public Class<? extends K> getKeyClass() 
	{
		return keyClass;
	}

	public Class<? extends V> getValueClass() 
	{
		return valueClass;
	}
}
